package com.fhz.module_main.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.fhz.module_main.R
import com.fhz.module_main.databinding.HomeItemFollowCardViewBinding
import com.fhz.module_main.databinding.RecommendRvBannerLayoutBinding
import com.fhz.module_main.entity.RecommendBanner
import com.fhz.module_main.entity.RecommendEntity
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

/**
 * 时间:2023/4/26
 * @author Mr.Feng
 * 简述: 推荐的适配器
 */
class RecommendAdapter (data: List<RecommendEntity.Item>) : BaseMultiItemAdapter<RecommendEntity.Item>(data) {

    // 类型 1 的 viewholder
    class ItemVH(val viewBinding: HomeItemFollowCardViewBinding) : RecyclerView.ViewHolder(viewBinding.root)

    // Banner 的 viewholder
    class BannerVH(val viewBinding: RecommendRvBannerLayoutBinding) : RecyclerView.ViewHolder(viewBinding.root)

    // 在 init 初始化的时候，添加多类型
    init {
        addItemType(ITEM_TYPE, object : OnMultiItemAdapterListener<RecommendEntity.Item, ItemVH> { // 类型 1

            lateinit var c: Context
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVH {
                // 创建 viewholder
                c = context
                val viewBinding = HomeItemFollowCardViewBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVH(viewBinding)
            }

            override fun onBind(holder: ItemVH, position: Int, item1: RecommendEntity.Item?) {
                // 绑定 item 数据
//                val item1 = data[position]

                if(item1?.type == "squareCardCollection"){
                    println(1111)
                if (item1 != null) {
                    println(item1.type)

                    println(item1.data.header.title)
                    println( item1.data.itemList[position].data.content.data.author.icon)
                    println( item1.data.itemList[position].data.content.data.title)

                    println(item1.data.header.icon)
                    Glide.with(c).load(item1.data.itemList[position].data.content.data.cover.feed).into(holder.viewBinding.ivVideoCover)
                    Glide.with(c).load(item1.data.itemList[position].data.header.icon).into(holder.viewBinding.ivAuthor)
                    holder.viewBinding.tvTitle.text = item1.data.itemList[position].data.header.title
                    holder.viewBinding.tvDescription.text = item1.data.itemList[position].data.header.description
                }
                }

            }
        }).addItemType(BANNER_TYPE, object : OnMultiItemAdapterListener<RecommendEntity.Item, BannerVH> { // 类型 2
            lateinit var c: Context
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): BannerVH {
                // 创建 viewholder
                c = context
                val viewBinding = RecommendRvBannerLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
                return BannerVH(viewBinding)
            }

            override fun onBind(holder: BannerVH, position: Int, item: RecommendEntity.Item?) {
                // 绑定 item 数据
                val bannerView = holder.viewBinding.bannerView
                bannerView.setAdapter(object : BaseBannerAdapter<RecommendBanner>() {
                    override fun bindData(
                        holder: BaseViewHolder<RecommendBanner>?,
                        data: RecommendBanner?,
                        position: Int,
                        pageSize: Int
                    ) {
                        val image = holder?.findViewById<ImageView>(R.id.banner_image)
                        if (image != null) {
                            Glide.with(image.context).load(data!!.imageUrl)
                                .apply(RequestOptions.bitmapTransform(RoundedCorners(20))).into(image)
                        }
                    }

                    override fun getLayoutId(viewType: Int): Int {
                        return  R.layout.recommend_banner_layout
                    }
                })

                bannerView.create()

                //填充刷新数据
                val bannerListOf = mutableListOf<RecommendBanner>()
                bannerListOf.add(RecommendBanner("http://img.kaiyanapp.com/5abd51b35e6f0cfb9c00b9b9417c703d.jpeg?imageMogr2/quality/60/format/jpg"))
                bannerListOf.add(RecommendBanner("http://img.kaiyanapp.com/f8d18532be27c053b8ac68922361b866.jpeg?imageMogr2/quality/60/format/jpg"))
                bannerListOf.add(RecommendBanner("http://img.kaiyanapp.com/5232f4b636b9a8840d0046aa8d143be2.png?imageMogr2/quality/60/format/jpg"))
                bannerView.refreshData(bannerListOf)
            }

        }).onItemViewType { position, list -> // 根据数据，返回对应的 ItemViewType
//                println(position)
            ITEM_TYPE
//            list.get(position).data.itemList[position].type
//            when (list[position].type) {
//                "squareCardCollection" -> { ITEM_TYPE}
//                "textCard" -> { BANNER_TYPE }
//                else -> {
//                    BANNER_TYPE
//                }
//            }
        }
    }

    companion object {
        private const val BANNER_TYPE = 0 //banner
        private const val ITEM_TYPE = 1  //广场精选


    }
}